<template>
	<Form ref="loginForm" :model="form" :rules="rules" @keydown.enter.native="handleSubmit">
		<FormItem prop="userName">
			<Input v-model="form.userName" placeholder="请输入用户名">
			<span slot="prepend">
				<Icon :size="16" type="ios-person"></Icon>
			</span>
			</Input>
		</FormItem>
		<FormItem prop="password">
			<Input type="password" v-model="form.password" placeholder="请输入密码">
			<span slot="prepend">
				<Icon :size="14" type="md-lock"></Icon>
			</span>
			</Input>
		</FormItem>
		<FormItem>
			<Button @click="handleSubmit" type="primary" long>登录</Button>
		</FormItem>
	</Form>
</template>
<script>
	import Util from '@/utils/index'
	export default {
		name: 'LoginForm',
		props: {
			userNameRules: {
				type: Array,
				default: () => {
					return [{
						required: true,
						message: '账号不能为空',
						trigger: 'blur'
					}]
				}
			},
			passwordRules: {
				type: Array,
				default: () => {
					return [{
						required: true,
						message: '密码不能为空',
						trigger: 'blur'
					}]
				}
			}
		},
		data () {
			return {
				form: {
					userName: 'liuyu',
					password: 'liuyu2006'
				},
				temp: {}
			}
		},
		computed: {
			rules () {
				return {
					userName: this.userNameRules,
					password: this.passwordRules
				}
			}
		},
		methods: {
			handleSubmit () {
				this.$api.post(0, '/usr/v2/reglog/login', {
					userName: this.form.userName,
					passWord: this.form.password
				}, r => {
					if (r.data.code === 0) {
						if (typeof r.data.data === 'string') {
							this.$Message.error(r.data.data)
						} else {
							Util.setToken(r.data.data.jwt.token_type + ' ' + r.data.data.jwt.access_token)
							this.temp = {
								id: r.data.data.uid,
								userName: r.data.data.userName,
								role: r.data.data.roles,
								menus: r.data.data.menus
							}
							let date = new Date()
							sessionStorage.tokenDate = date.getTime()
							sessionStorage.user = JSON.stringify(this.temp)
							this.$router.push({
								name: 'home'
							})
						}
					}
				})
			}
		}
	}
</script>
